package siwi.map.android;

import siwi.map.android.Login.LoginFailureHandler;

import com.actionbarsherlock.app.SherlockActivity;
import com.wmw.Animation.EnlargeAnimationOut;
import com.wmw.Animation.ShrinkAnimationOut;
import com.wmw.Animation.ViewAction;
import com.wmw.DataPool.CommonData;
import com.wmw.Utility.Common;
import com.wmw.Utility.MSG;
import com.wmw.Utility.RegularExpression;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnticipateInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class RegisterFirstStep extends SherlockActivity implements
		OnClickListener {
	private InputBox _username, _password, _ensurePassword;
	private EditText view_userName;
	private EditText view_password;
	private EditText view_passwordConfirm;
	private Button view_submit;
	private Button view_clearAll;
	private static final int MENU_EXIT = Menu.FIRST - 1;
	private static final int MENU_ABOUT = Menu.FIRST;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_first_step);

		final EditText etUsername = (EditText) findViewById(R.id.RFS_Username);
		ImageView clearUsername = (ImageView) findViewById(R.id.RFS_ClearUsername);
		_username = new InputBox(this, etUsername, clearUsername, null,
				etUsername.getPaddingRight(), RegularExpression.Username,
				new InputBox.ShakeBox() {
					@Override
					public void Action(EditText et, String regularExpression,
							Context context, View v, boolean hasFocus) {
						super.Action(et, regularExpression, context, v,
								hasFocus);
					}

					@Override
					public void OnFocusChange(boolean valide, boolean hasFocus) {
						super.OnFocusChange(valide, hasFocus);
						Log.d(Common.GetLineInfo(), "Focus -" + hasFocus);
						if (hasFocus)
							etUsername
									.setBackgroundColor(RegisterFirstStep.this
											.getResources().getColor(
													R.color.TextBackground));
						else if (valide)
							etUsername
									.setBackgroundColor(RegisterFirstStep.this
											.getResources().getColor(
													R.color.Transparent));
					}
				});

		final EditText etPassword = (EditText) findViewById(R.id.RFS_Password);
		ImageView clearPassword = (ImageView) findViewById(R.id.RFS_ClearPassword);
		_password = new InputBox(this, etPassword, clearPassword, null,
				etPassword.getPaddingRight(), RegularExpression.Password,
				new InputBox.ShakeBox() {
					@Override
					public void Action(EditText et, String regularExpression,
							Context context, View v, boolean hasFocus) {
						super.Action(et, regularExpression, context, v,
								hasFocus);
					}

					@Override
					public void OnFocusChange(boolean valide, boolean hasFocus) {
						super.OnFocusChange(valide, hasFocus);
						Log.d(Common.GetLineInfo(), "Focus -" + hasFocus);
						if (hasFocus)
							etPassword
									.setBackgroundColor(RegisterFirstStep.this
											.getResources().getColor(
													R.color.TextBackground));
						else if (valide)
							etPassword
									.setBackgroundColor(RegisterFirstStep.this
											.getResources().getColor(
													R.color.Transparent));
					}
				});

		final EditText etEnsurePassword = (EditText) findViewById(R.id.RFS_EnsurePassword);
		ImageView clearEnsurePassword = (ImageView) findViewById(R.id.RFS_ClearEnsurePassword);
		_ensurePassword = new InputBox(this, etEnsurePassword,
				clearEnsurePassword, null, etEnsurePassword.getPaddingRight(),
				RegularExpression.Password, new InputBox.ShakeBox() {
					@Override
					public void Action(EditText et, String regularExpression,
							Context context, View v, boolean hasFocus) {
						super.Action(et, regularExpression, context, v,
								hasFocus);
					}

					@Override
					public void OnFocusChange(boolean valide, boolean hasFocus) {
						super.OnFocusChange(valide, hasFocus);
						Log.d(Common.GetLineInfo(), "Focus -" + hasFocus);
						if (hasFocus)
							etEnsurePassword
									.setBackgroundColor(RegisterFirstStep.this
											.getResources().getColor(
													R.color.TextBackground));
						else if (valide)
							etEnsurePassword
									.setBackgroundColor(RegisterFirstStep.this
											.getResources().getColor(
													R.color.Transparent));
					}
				});

		Button button = (Button) findViewById(R.id.RFS_NextStep);
		button.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		ViewAction.startSpringMenuAnimations(v);

		if (!_username.isValide()) {
			Toast.makeText(RegisterFirstStep.this, "请输入正确的用户名",
					Toast.LENGTH_SHORT).show();
			return;
		}

		if (!_password.isValide()) {
			Toast.makeText(RegisterFirstStep.this, "请输入正确的密码",
					Toast.LENGTH_SHORT).show();
			return;
		}

		if (!_ensurePassword.getContent().equals(_password.getContent())) {
			Toast.makeText(RegisterFirstStep.this, "两次密码不一致",
					Toast.LENGTH_SHORT).show();
			return;
		}

		// TODO
		// Thread registerThread = new Thread(new RegisterFailureHandler());
		// registerThread.start();

		Common.toastInfo(RegisterFirstStep.this, "下一步");
		Intent intent = new Intent();
		intent.setClass(RegisterFirstStep.this, RegisterSecondStep.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(intent);
	}

}
//
// /** 1.初始化注册view组件 */
// private void findViews() {
// view_userName = (EditText) findViewById(R.id.registerUserName);
// view_password = (EditText) findViewById(R.id.registerPassword);
// view_passwordConfirm = (EditText) findViewById(R.id.registerPasswordConfirm);
// view_submit = (Button) findViewById(R.id.registerSubmit);
// view_clearAll = (Button) findViewById(R.id.registerClear);
// }
//
// private void setListener() {
// view_submit.setOnClickListener(submitListener);
// view_clearAll.setOnClickListener(clearListener);
// }
//
// /** 监听注册确定按钮 */
// private OnClickListener submitListener = new OnClickListener() {
// @Override
// public void onClick(View v) {
// String userName = view_userName.getText().toString();
// String password = view_password.getText().toString();
// String passwordConfirm = view_passwordConfirm.getText().toString();
// validateForm(userName, password, passwordConfirm);
// }
// };
//
// /** 清空监听按钮 */
// private OnClickListener clearListener = new OnClickListener() {
// @Override
// public void onClick(View v) {
// clearForm();
// }
// };
//
// /** 检查注册表单 */
// private void validateForm(String userName, String password, String password2)
// {
// StringBuilder suggest = new StringBuilder();
// Log.d(this.toString(), "validate");
// if (userName.length() < 1) {
// suggest.append(getText(R.string.suggust_userName) + "\n");
// }
// if (password.length() < 1 || password2.length() < 1) {
// suggest.append(getText(R.string.suggust_passwordNotEmpty) + "\n");
// }
// if (!password.equals(password2)) {
// suggest.append(getText(R.string.suggest_passwordNotSame));
// }
// if (suggest.length() > 0) {
// //sub是因为要出去最后一个 \n换行符
// Toast.makeText(this, suggest.subSequence(0, suggest.length() - 1),
// Toast.LENGTH_SHORT).show();
// }
// }
//
// /** 清空表单 */
// private void clearForm() {
// view_userName.setText("");
// view_password.setText("");
// view_passwordConfirm.setText("");
//
// view_userName.requestFocus();
// }
//
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// super.onCreateOptionsMenu(menu);
// menu.add(0, MENU_EXIT, 0, R.string.MENU_EXIT);
// menu.add(0, MENU_ABOUT, 0, R.string.MENU_ABOUT);
// return true;
// }
//
// @Override
// public boolean onMenuItemSelected(int featureId, MenuItem item) {
// super.onMenuItemSelected(featureId, item);
// switch (item.getItemId()) {
// case MENU_EXIT:
// finish();
// break;
// case MENU_ABOUT:
// alertAbout();
// break;
// }
// ;
// return true;
// }
//
// /** 弹出关于对话框 */
// private void alertAbout() {
// new AlertDialog.Builder(Register.this).setTitle(R.string.MENU_ABOUT)
// .setMessage(R.string.aboutInfo).setPositiveButton(
// R.string.ok_label,
// new DialogInterface.OnClickListener() {
// public void onClick(
// DialogInterface dialoginterface, int i) {
// }
// }).show();
// }
// }
